package com.axjy.item.task;

import com.axjy.comm.pojo.TpCinema;
import com.axjy.comm.pojo.TpImage;
import com.axjy.comm.pojo.TpItem;
import com.axjy.comm.pojo.TpItemType;
import com.axjy.item.feign.ImageFeignClient;
import com.axjy.item.mapper.CinemaMapper;
import com.axjy.item.mapper.ItemMapper;
import com.axjy.item.mapper.ItemTypeMapper;
import com.axjy.item.vo.FloorKeyVo;
import com.axjy.item.vo.ItemVo;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Component
public class FloorItemAndDetailItemTask {

    @Resource
    private RedissonClient redissonClient;

    @Resource
    private ItemTypeMapper itemTypeMapper;

    @Resource
    private ItemMapper itemMapper;

    @Resource
    private CinemaMapper cinemaMapper;

    @Resource
    private ImageFeignClient imageFeignClient;

    private void executeTask(){
        RMap<FloorKeyVo, List<ItemVo>> map=redissonClient.getMap("tp:item:floor");
        List<TpItemType> tpItemTypes=itemTypeMapper.findByKey();
        for (TpItemType itemType:tpItemTypes){
            FloorKeyVo floorKeyVo=new FloorKeyVo();
            //==============================================================
            floorKeyVo.setFloor(Integer.parseInt(itemType.getKey()));
            floorKeyVo.setItemTypeId(itemType.getId());
            floorKeyVo.setItemTypeName(itemType.getAliasName());
            //==============================================================
            List<TpItem> list=itemMapper.find4Floor(floorKeyVo.getItemTypeId(),5);
            List<ItemVo> itemVoList = list.stream().map(tpItem -> {
                ItemVo itemVo = new ItemVo();
                BeanUtils.copyProperties(tpItem,itemVo);
                TpCinema tpCinema = cinemaMapper.selectById(tpItem.getCinemaId());
                itemVo.setCinemaId(tpCinema.getId());
                BeanUtils.copyProperties(tpCinema,itemVo);//tpCinema也有一个id
                itemVo.setId(tpItem.getId());
                TpImage tpImage = imageFeignClient.findByTargetId(tpItem.getId(),2,1);
                if(tpImage != null){
                   itemVo.setImgUrl(tpImage.getImgUrl());
                }else{
                   itemVo.setImgUrl("https://img-bss.csdnimg.cn/2020852192818_25789.jpg?imageMogr2/auto-orient/thumbnail/360x360!/format/jpg%7Cwatermark/1/image/aHR0cHM6Ly9pbWctYnNzLmNzZG5pbWcuY24vYkphdmFfOS5wbmc=/dissolve/85/gravity/Center/dx/0/dy/0/");
                }
                return itemVo;
            }).collect(Collectors.toList());
            map.put(floorKeyVo,itemVoList);
        }
    }
    @Scheduled(initialDelay = 1000,fixedDelay = Long.MAX_VALUE)
    public void initialTask(){
        executeTask();
    }
    @Scheduled(cron = "0 0 2 * * ?")
    public void cronTask(){
        executeTask();
    }
}